<template>
  <div class="app-container">
    <div class="query-panel">
      <div class="query-panel-filter">
        <div class="query-form">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="类型" prop="code">
              <el-input v-model="queryParams.code" placeholder="请输入类型" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-form>
        </div>
        <div class="query-btn">
          <el-row>
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-query-add" class="c-item-btn" size="mini" @click="handleAdd"
                v-hasPermi="['basic:datadictype:add']">新增</el-button>
              <el-button class="c-item-btn" icon="el-icon-refresh" size="mini" @click="handleSync">同步数据</el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="query-search">
        <div class="query-search-btn">
          <div class="query-search-btn">
            <el-button class="c-btn-reset" icon="el-icon-query-reset" size="mini" @click="resetQuery">重置</el-button>
            <el-button class="c-btn-search" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </div>
        </div>
      </div>
    </div>

    <el-table v-loading="loading" :data="typeList" stripe>
      <el-table-column label="类型名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="类型代码" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="{
            path: '/basic/dict-data/index/' + scope.row.id,
            query: { dictype: scope.row.code },
          }" class="link-type">
            <span>{{ scope.row.code }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-view" @click="handleView(scope.row)"
            v-hasPermi="['basic:datadictype:view']">查看</el-button>
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:datadictype:edit']">修改</el-button>
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['basic:datadictype:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="670px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" :disabled="disabled">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" size="small" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="类型" prop="code">
          <el-input v-model="form.code" size="small" placeholder="请输入字典类型" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="cancel-btn">取 消</el-button>
        <el-button v-if="!disabled" class="save-btn" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDatadictype,
  getDatadictype,
  getSyncData,
  delDatadictype,
  addDatadictype,
  updateDatadictype,
} from "@/api/basic/datadictype";

export default {
  name: "Dicttype",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        orderByColumn: "id",
        isAsc: "desc",
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "类型不能为空", trigger: "blur" }],
      },
      disabled: false,
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      listDatadictype(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        code: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.open = true;
      this.title = "添加类型";

      this.$nextTick(() => {
        this.dialogIcon("add");
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDatadictype(id).then((response) => {
        this.form = response.data;
        this.disabled = false;
        this.open = true;
        this.title = "修改类型";

        this.$nextTick(() => {
          this.dialogIcon("edit");
        });
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getDatadictype(id).then((response) => {
        this.form = response.data;
        this.disabled = true;
        this.open = true;
        this.title = "查看类型";

        this.$nextTick(() => {
          this.dialogIcon("view");
        });
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateDatadictype(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDatadictype(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除字典编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delDatadictype(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleSync() {
      getSyncData({}).then((response) => {
        this.msgSuccess("同步成功");
      });
    },
  },
};
</script>